# -*-coding: Utf-8 -*-
# @File : models .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2023/10/7
from sqlalchemy import Column, String, Integer, BigInteger, Date, DateTime, ForeignKey, func
from sqlalchemy.orm import relationship
from FastapiTest.database import Base


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    username = Column(String(32), unique=True, nullable=False, comment="用户名")
    password = Column(String(32), nullable=False, comment="密码")
    phone = Column(String(100), nullable=False, comment="电话")
    email = Column(BigInteger, nullable=False, comment="邮箱")
    # 添加关联关系，back_populates 用于指定反向关系
    phones = relationship("UserPhone", back_populates="user")
    emails = relationship("UserEmail", back_populates="user")


class UserPhone(Base):
    __tablename__ = 'user_phone'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    phone = Column(String(32), unique=True, nullable=False, comment="电话")

    # 定义外键，指向 User 表的主键
    user_id = Column(Integer, ForeignKey("user.id"))
    user = relationship("User", back_populates="phones")


class UserEmail(Base):
    __tablename__ = 'user_email'
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    email = Column(String(32), unique=True, nullable=False, comment="邮箱")

    # 定义外键，指向 User 表的主键
    user_id = Column(Integer, ForeignKey("user.id"))
    user = relationship("User", back_populates="emails")
